package com.dyh.quickdemozwei;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.FiberForkJoinScheduler;
import co.paralleluniverse.fibers.FiberScheduler;
import co.paralleluniverse.fibers.Suspendable;
import co.paralleluniverse.fibers.httpclient.FiberHttpClientBuilder;
import com.dyh.quickdemozwei.util.HttpClientCustom;
import com.dyh.quickdemozwei.util.HttpClientWrap;
import com.dyh.quickdemozwei.util.HttpClinetCallRecipes;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Slf4j
public class TestQuasarBase {

    private FiberScheduler scheduler =
            new FiberForkJoinScheduler("dyh-FiberScheduler", 1, null, false);


    @Test
    public void test() {
        int n = 4;
        List<Fiber<String>> fibers = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int finalI = i;
            Fiber<String> fiber = new Fiber<>("name", scheduler, () -> {
                log.info("{} 开始 i={}", Thread.currentThread().getName(), finalI);

                Fiber.sleep(1000L);

                log.info("{} 结束 i={}", Thread.currentThread().getName(), finalI);

                return String.format("%s#%s", finalI, "");
            });
            fiber.start();
            fibers.add(fiber);
        }
        for (Fiber<String> fiber : fibers) {
            try {
                Object res = fiber.get();
                log.info("res={}", res);
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


}
